package com.businessportal.web.dataaccess.dao;

import com.businessportal.web.common.DateTimeHelper;
import com.businessportal.web.dataaccess.entities.Event;
import com.businessportal.web.dataaccess.entities.Login;
import com.businessportal.web.dataaccess.utils.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by Vadim on 7/4/14.
 */
public class EventDao implements IEventDao {

    @Override
    public Event getById(Long aLong) {
        Session session = null;
        Event retValue = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            retValue = (Event)session.get(Event.class, aLong);

            session.getTransaction().commit();
        } catch (Exception e){

            retValue = null;
            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;
    }

    @Override
    public List<Event> getAll() {
        Session session = null;
        List<Event> retValue = null;

        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            Query userQuery = session.createQuery("From Login where 1=1");

            retValue = userQuery.list();

            session.getTransaction().commit();

        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;
    }

    @Override
    public void save(Event event) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.save(event);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void update(Event event) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.saveOrUpdate(event);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void delete(Event event) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.delete(event);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void deleteById(Long aLong) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.delete(new Event(aLong));

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public Set<Event> getEventsForUser(Login user) {
        Session session = null;
        Set<Event> retValue = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            String sql = String.format("from Event where owner_id = %s", user.getId());

            Query blogQuery = session.createQuery(sql);

            List<Event> retList = blogQuery.list();

            retValue = new TreeSet<Event>(retList);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;
    }

    @Override
    public Set<Event> getPublicEventsForUser(Login user) {
        Session session = null;
        Set<Event> retValue = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            String sql = String.format("from Event where owner_id = %s and is_public = true", user.getId());

            Query blogQuery = session.createQuery(sql);

            List<Event> retList = blogQuery.list();

            retValue = new TreeSet<Event>(retList);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;

    }

    @Override
    public Set<Event> getEventWithNotification() {
        Session session = null;
        Set<Event> retValue = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            String sql = "from Event where notify_me = true and when_notify_me <= :now";

            Query blogQuery = session.createQuery(sql);
            blogQuery.setTimestamp("now", new Date());
            blogQuery.setMaxResults(100);

            List<Event> retList = blogQuery.list();

            retValue = new TreeSet<Event>(retList);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;
    }
}
